package com.ucs.ui;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class TableListAdaptor extends BaseExpandableListAdapter{

	@Override
	public Object getChild(int arg0, int arg1) {
		return arg1+1;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parentView) {
		TextView textView = (TextView)convertView;
		if (textView == null){
			textView = CategoryItemListAdaptor.getGenericView(parentView);
		}
		int tableNumber = (Integer)getChild(groupPosition, childPosition);
		textView.setText(""+tableNumber);            
		return textView;        
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 99;
	}

	public static final char[] tableLetters = {'A','B','C','D','E','F','H'};
	@Override
	public Object getGroup(int groupPosition) {
		return tableLetters[groupPosition];
	}

	@Override
	public int getGroupCount() {
		return tableLetters.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parentView) {
		TextView textView = (TextView)convertView;
		if (textView == null){
			textView = CategoryItemListAdaptor.getGenericView(parentView);
		}
		char tableLetter = (Character)getGroup(groupPosition);
		textView.setText(""+tableLetter);            
		return textView;        
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	static public String getTableName(int tableID){
		int groupNumber = tableID/100;
		int tableNumber = tableID%100;
		return ""+tableLetters[groupNumber]+tableNumber;
	}
}
